package nullby.yoga.activity;

import java.util.Arrays;
import java.util.Locale;

import nullby.yoga.R;
import nullby.yoga.utils.Extra;
import nullby.yoga.utils.PrefManager;
import nullby.yoga.utils.SoundManager;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.Toast;

public class SettingsActivity extends CommonActivity implements OnClickListener {
	private static final String TAG = SettingsActivity.class.getSimpleName();
	private CheckBox checkBox;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
//		initView();
	}

	@Override
	protected void initView() {
		setContentView(R.layout.settings);
		findViewById(R.id.SettingsLanguage).setOnClickListener(this);
		findViewById(R.id.SettingsButSound).setOnClickListener(this);
		findViewById(R.id.SettingsButSoundChBox).setOnClickListener(this);
		findViewById(R.id.SettingsFeedback).setOnClickListener(this);
		findViewById(R.id.SettingsReview).setOnClickListener(this);
		findViewById(R.id.SettingsWebsite).setOnClickListener(this);
		findViewById(R.id.SettingsAbout).setOnClickListener(this);
		checkBox = (CheckBox) findViewById(R.id.SettingsButSoundChBox);
		checkBox.setChecked(PrefManager.get().isButtonSound());
	}

	@Override
	protected void onResume() {
		super.onResume();
		setLang();
//		setContentView(R.layout.settings);
		initView();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.SettingsLanguage:
			SoundManager.getInstance().playClick();
			final CharSequence[] lang = getResources().getStringArray(
					R.array.lang);
			final CharSequence[] langValue = getResources().getStringArray(
					R.array.langValue);

			int checkedItem = Arrays.asList(langValue).indexOf(
					(!"".equals(PrefManager.get().getLang())) ? PrefManager
							.get().getLang() : "en");

			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setSingleChoiceItems(lang, checkedItem,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int item) {
							PrefManager.get().putLang((String) langValue[item]);
							onResume();
							dialog.dismiss();
						}
					});

			AlertDialog alert = builder.create();
			alert.show();
			break;
		case R.id.SettingsButSound:
			check(!checkBox.isChecked());
			break;
		case R.id.SettingsButSoundChBox:
			check(checkBox.isChecked());
			break;
		case R.id.SettingsFeedback:
			toUrl(getResources().getString(R.string.url_feedback));
			break;
		case R.id.SettingsReview:
			toUrl(getResources().getString(R.string.url_review));
			break;
		case R.id.SettingsWebsite:
			toUrl(getResources().getString(R.string.url_website));
			break;
		case R.id.SettingsAbout:
			toUrl(getResources().getString(R.string.url_about));
			break;
		default:
			break;
		}
	}

	private void check(final Boolean isChecked) {
		PrefManager.get().setButtonSound(isChecked);
//		PrefManager.get().getEditor().commit();
		checkBox.setChecked(PrefManager.get().isButtonSound());
		SoundManager.getInstance().playCheck();
	}

	private void toUrl(final String url) {
		SoundManager.getInstance().playClick();
		Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
		startActivity(browserIntent);
	}
}
